package com.eorchis.module.basedata.dao.impl;

import com.eorchis.core.basedao.base.IBaseDao;
import com.eorchis.core.basedao.condition.BaseCondition;
import com.eorchis.module.basedata.dao.IBaseDataDao;
import com.eorchis.module.basedata.dao.require.BaseDataRequire;
import com.eorchis.module.basedata.domain.BaseData;
import com.eorchis.module.basedata.domain.BaseDataCondition;
import com.eorchis.module.basedata.domain.BaseDataTypeCondition;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

@Repository("com.eorchis.module.basedata.dao.impl.BaseDataDaoImpl")
public class BaseDataDaoImpl
  implements IBaseDataDao
{

  @Autowired
  @Qualifier("com.eorchis.core.basedao.base.impl.BaseDao")
  private IBaseDao<BaseData> baseDao;

  @Autowired
  @Qualifier("com.eorchis.module.basedata.dao.require.BaseDataRequire")
  private BaseDataRequire baseDataRequire;

  public void discardOrReuseBaseData(BaseDataCondition condition)
    throws Exception
  {
    this.baseDao.executeUpdate(this.baseDataRequire.discardOrReuseBaseDataHQL(condition));
  }

  public BaseData getBaseData(BaseDataCondition condition)
    throws Exception
  {
    return (BaseData)this.baseDao.findObject(BaseData.class, condition.getSearchDataId());
  }

  public List<BaseData> getBaseDataList(BaseDataCondition condition)
    throws Exception
  {
    this.baseDataRequire.getBaseDataListHql(condition);

    return (List<BaseData>) this.baseDao.find(condition);
  }

  public void updateBaseData(BaseData baseData)
    throws Exception
  {
    this.baseDao.updateEntityByPK(baseData);
  }

  public void addBaseData(BaseData baseData)
    throws Exception
  {
    this.baseDao.addEntity(baseData);
  }

  public boolean checkDataCodeIsRepeat(BaseData baseData)
    throws Exception
  {
    boolean repeat = false;
    Session session = this.baseDao.getHibernateSession();
    Query query = session.createQuery(this.baseDataRequire.checkDataCodeIsRepeatHql(baseData));

    this.baseDataRequire.checkDataCodeIsRepeatParameter(baseData, query);
    Long validation = (Long)query.uniqueResult();
    if (validation.intValue() > 0) {
      repeat = true;
    }
    return repeat;
  }

  public BaseData viewBaseData(BaseDataCondition condition)
    throws Exception
  {
    this.baseDataRequire.viewBaseDataHql(condition);
    List result = this.baseDao.find(condition.getSql(), condition.getParameterList().toArray());
    if ((result != null) && (result.size() > 0)) {
      return (BaseData)result.get(0);
    }
    return null;
  }

  public List<BaseData> listBaseData(BaseDataCondition condition)
    throws Exception
  {
    this.baseDataRequire.listBaseData(condition);
    return (List)this.baseDao.pagedQuery(condition);
  }

  public Long listBaseDataCount(BaseDataCondition condition)
    throws Exception
  {
    this.baseDataRequire.countBaseDataHql(condition);
    return Long.valueOf(this.baseDao.countQuery(condition));
  }

  public List<BaseData> getAllBaseDataByTypeID(BaseDataTypeCondition condition)
    throws Exception
  {
    this.baseDataRequire.queryBaseDataByTypeID(condition);
    return (List<BaseData>) this.baseDao.find(condition.getSql(), condition.getParameterList().toArray());
  }

  public BaseData getBaseDataByID(String id)
    throws Exception
  {
    BaseData baseData = (BaseData)this.baseDao.findObject(BaseData.class, id);
    this.baseDao.getHibernateSession().clear();
    return baseData == null ? new BaseData() : baseData;
  }

  public List<BaseData> getBaseDataListByIDArray(String[] idArray)
    throws Exception
  {
    this.baseDao.getHibernateSession().clear();
    BaseCondition condition = this.baseDataRequire.getBaseDataListByIDArrayHQL(idArray);
    return (List<BaseData>) this.baseDao.find(condition);
  }
}